<?php

namespace Common\Model;

use Think\Model;

class UserBanModel extends Model
{
    /**
     * 获取用户禁言信息
     *
     * 如果用户当前未被禁言，则返回False
     *
     * @param string $uid
     * @return array|bool
     */
    public function chenk_ban($uid = '')
    {
        $map['uid'] = intval($uid);
        $ban = $this->where($map)->find();
        if (!empty($ban)) {
            $time = time();
            if ($ban['endtime'] > $time || $ban['endtime'] == 0) {
                $ban['addtime_str'] = date("Y-m-d H:i", $ban['addtime']);
                if ($ban['endtime'] == 0) {
                    $ban['endtime_str'] = '永久';
                } else {
                    $ban['endtime_str'] = date("Y-m-d H:i", $ban['endtime']);
                }
                return $ban;
            }
        }
        return false;
    }

    /**
     * 清除禁言
     *
     * @param string $uid
     * @return bool
     */
    public function clear_ban($uid = '')
    {
        $map['uid'] = array('IN', $uid);
        $this->where($map)->delete();
        return true;
    }

    /**
     * 设置禁言
     *
     * @param string $uid
     * @param string $day
     * @param string $txt
     * @return int
     */
    public function set_ban($uid = '', $day = '1', $txt = '')
    {
        $this->clear_ban($uid);
        $s = 0;
        $day = intval($day);
        $data['addtime'] = time();
        if ($day == 0) {
            $data['endtime'] = 0;
        } else {
            $data['endtime'] = strtotime("+{$day} day");
        }
        $data['txt'] = trim($txt);
        $uidarray = explode(",", $uid);
        if (!empty($uidarray)) {
            foreach ($uidarray as $u) {
                $data['uid'] = $u;
                $this->add($data);
                $s++;
            }

        }
        return $s;
    }
}